package z.ftpd;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;

import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.util.*;

public final class MainActivity extends FtpActivity {


	@Override
	protected void stop() {
		var ftpd = (FtpServer) server;
		ftpd.stop();
	}

	@Override
	protected void start() {
		var serverFactory = new FtpServerFactory();
		var ftpd = serverFactory.createServer();
		var factory = new ListenerFactory();
		factory.setPort(port);
		serverFactory.addListener("default", factory.createListener());
		var user = new BaseUser() {{
			setName("anonymous");
			setAuthorities(Collections.singletonList(new WritePermission()));
		}};
		user.setHomeDirectory(sd);
		try {
			serverFactory.getUserManager().save(user);
			ftpd.start();
		} catch (FtpException e) {
			popup(e.getMessage());
		}
		server = ftpd;
	}
}